/**
 * 
 */
package com.fish.web.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.fish.common.constant.Constant;
import com.fish.common.constant.QueryPage;
import com.fish.common.web.BaseAction;
import com.fish.service.user.MenusService;
import com.fish.service.user.UserService;
import com.fish.user.po.Menus;
import com.fish.user.po.TreeBean;
import com.fish.user.po.User;

/**
 * @author damian
 *
 */
public class UserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6481667003758108072L;
	
	private UserService userService;
	private List<User> userList = new ArrayList<User>();
	private User userQueryForm = new User();
	private User userForm;
	private String userId;
	private User userBean;
	private User user = new User();
	private String message;
	private MenusService menusService;
	private QueryPage queryPage = new QueryPage();
	private TreeBean<Menus> menuTree = new TreeBean<Menus>();
	
	public String goToUser(){
		return SUCCESS;
	}
	
	public String login() {
		if(user != null) {
			if (user.getUserName()==null && user.getPassWord()==null) {
				return SUCCESS;
			} else {
				if (user.getUserName().trim().equals("") || user.getPassWord().trim().equals("")) {
					return SUCCESS;
				}
			}
			User userDB = userService.getUserByUser(user);
			if (userDB!=null) {
				//用户权限列表
				menuTree =menusService.queryMenusByUser(userDB);
				userDB.setMenuTree(menuTree);		
				getRequest().getSession().setAttribute(Constant.SESSION_BACK_USER_KEY, userDB);
				
				//return "index";
				return "common";
			}
			message="用户名不存在，或密码错误";
		}
		return SUCCESS;	
	}
	
	public String logout(){
		super.removeSessionUser();
		return SUCCESS;
	}
	
	public String goToChangePassword(){
		return SUCCESS;
	}
	
	public String changePassword(){
		User userInDb=userService.selectUserById(userForm.getUserId());
		userInDb.setPassWord(userForm.getPassWord());
		userService.updateUser(userInDb);
		return SUCCESS;
	}
	
	/**
	 * 查询列表
	 * @return
	 */
	public String list(){
		Long count = userService.queryUserCountByObject(userQueryForm);
		if(count > 0){
			queryPage = new QueryPage(count, Constant.DEFAULT_PAGE_SIZE, queryPage.getCurPage());
		}
		
		userList = userService.queryUserByObject(userQueryForm,queryPage);
		return SUCCESS;
	}
	
	/**
	 * 进入新增页面
	 * @return
	 */
	public String create(){
		if(this.userBean==null){
			this.userBean=new User();
		}
		return SUCCESS;
	}
	
	/**
	 * 保存
	 * @return
	 */
	public String save(){
		if(this.userForm!=null){
			if(!"USER_TYPE_SYSTEM".equals(this.userForm.getUserType())){
				this.userForm.setIsAdmin("false");
			}
//			this.userForm.setPostUserId(super.getCurrentUserId());
			this.userForm.setUserId(super.getNewUUID());
			if(this.userService.insertUser(this.userForm)!=null){
//				this.addValueToRequest(retMsg,saveSuccess);
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 进入编辑页面
	 * @return
	 */
	public String edit(){
		this.userBean=this.userService.selectUserById(this.userId);
		return SUCCESS;
	}
	
	/**
	 * 更新
	 * @return
	 */
	public String update(){
		if(this.userForm!=null){
			if(!"USER_TYPE_SYSTEM".equals(this.userForm.getUserType())){
				this.userForm.setIsAdmin("false");
			}
//			this.userForm.setUpdateUserId(super.getCurrentUserId());
			if(this.userService.updateUser(this.userForm)){
//				this.addValueToRequest(retMsg,updateSuccess);
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
//		if(this.userService.deleteUser(this.userId,super.getCurrentUserId())){
////			this.addValueToRequest(retMsg,deleteSuccess);
//		}
		return SUCCESS;
	}

	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUserQueryForm() {
		return userQueryForm;
	}

	public void setUserQueryForm(User userQueryForm) {
		this.userQueryForm = userQueryForm;
	}

	public User getUserForm() {
		return userForm;
	}

	public void setUserForm(User userForm) {
		this.userForm = userForm;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public User getUserBean() {
		return userBean;
	}

	public void setUserBean(User userBean) {
		this.userBean = userBean;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public QueryPage getQueryPage() {
		return queryPage;
	}

	public void setQueryPage(QueryPage queryPage) {
		this.queryPage = queryPage;
	}

	public void setMenusService(MenusService menusService) {
		this.menusService = menusService;
	}

	public TreeBean<Menus> getMenuTree() {
		return menuTree;
	}

	public void setMenuTree(TreeBean<Menus> menuTree) {
		this.menuTree = menuTree;
	}
	
}
